﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditLocationEdge : BaseDataVisualizer, ISingleItemDataVisualizer
    {
        public override void SetVisualizedData(DataVisualizer vis, object o) { LocationEdge = o as LocationEdge; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(LocationEdge); } }
        public string VisualizerName { get { return "EditLocationEdge"; } }

        public static readonly DependencyProperty LocationEdgeProperty = DependencyProperty.Register(
            "LocationEdge", typeof(LocationEdge), typeof(EditLocationEdge), new PropertyMetadata(null, new PropertyChangedCallback(LocationEdgeChanged)));

        public LocationEdge LocationEdge
        {
            get { return (LocationEdge)GetValue(LocationEdgeProperty); }
            set { SetValue(LocationEdgeProperty, value); }
        }

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<EditLocationEdge, T2> action)
        {
            d.HandleChange<EditLocationEdge, T2>(e, action, (t) => t.UpdateBindings());
        }

        private static void LocationEdgeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandleChange<LocationEdge>(d, e, (t, val) => t.LocationEdge = val);
        }

        public override void UpdateBindings()
        {
            if (LocationEdge != null)
            {
                gridData.DataContext = LocationEdge;
            }
        }

        public override string TitleText
        {
            get
            {
                string result = string.Empty;

                if (LocationEdge == null)
                    return result;

                if (LocationEdge.ToGameLocation == null)
                    result = string.Format("LocationEdge {0} to {1}", LocationEdge.Direction, LocationEdge.ToLocationId);
                else
                    result = string.Format("LocationEdge {0} to {1}", LocationEdge.Direction, LocationEdge.ToGameLocation.XYZ);

                return result;
            }
        }

        public EditLocationEdge()
        {
            InitializeComponent();
        }

        public override void OnSaveData()
        {
            DataAccess.Client.StoreLocationEdgeAsync(DataAccess.AssignedUniqueId, LocationEdge);
        }

        private void ShowProps_Loaded(object sender, RoutedEventArgs e)
        {
            //editLocationEdgeProperties.Window.Indent = Window.Indent + 1;         
        }

        private void EditAssociatedIDCollection_RequestEditID(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnRequestEditID(sender, e);
        }
    }
}